package com.shycloud.mido.member.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.member.api.entity.DistDistributor;
import com.shycloud.mido.member.api.vo.DistDistributorVO;
import com.shycloud.mido.member.service.DistDistributorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 经销商业务 controller
 *
 * @author nianhua.jiang
 * @date 2021/6/30 9:59
 */
@RestController
@AllArgsConstructor
@Api(tags = "经销商业务")
@RequestMapping("distributor")
@ApiIgnore
public class DistributorController {

	/** 经销商业务 service */
	private final DistDistributorService distributorService;

	@ApiOperation("添加经销商")
	@PostMapping("addDistributor")
	public R addDistributor(@RequestBody DistDistributorVO distributor) {
		return new R(distributorService.addDistributor(distributor));
	}

	@ApiOperation("根据ID获取经销商详情")
	@GetMapping("getDistributorById")
	public R getDistributorById(Integer id) {
		return new R(distributorService.getById(id));
	}

	@ApiOperation("分页获取经销商信息")
	@GetMapping("getDistributorPage")
	public R getDistributorPage(Integer current, Integer size, Integer optId, String name,
			String phone, String province, String city) {

		QueryWrapper qw = new QueryWrapper<DistDistributor>();

		if (optId != null) {
			qw.eq("opt_id", optId);
		}
		if (StringUtils.isNotEmpty(name)) {
			qw.like("name", name);
		}
		if (StringUtils.isNotEmpty(phone)) {
			qw.like("phone", phone);
		}
		if (StringUtils.isNotEmpty(province)) {
			qw.eq("province", province);
		}
		if (StringUtils.isNotEmpty(city)) {
			qw.eq("city", city);
		}

		return new R(distributorService.page(new Page<>(current, size), qw));
	}

	@ApiOperation("更新经销商信息")
	@PostMapping("updDistributor")
	public R updDistributor(@RequestBody DistDistributor distributor) {
		return new R(distributorService.updateById(distributor));
	}
}
